import { Component, OnInit } from '@angular/core';
import { RescueDrill } from './bean/rescueDrill.bean';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { NzModalService, NzModalSubject } from 'ng-zorro-antd';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { HttpServcie } from '../../../service/http.service';
import { NzNotificationService } from 'ng-zorro-antd';
import { Config } from '../../../service/config';

@Component({
  selector: 'rescue-drill',
  templateUrl: './rescueDrill.html',
  styleUrls: ['./rescueDrill.scss']
})
/**
 * 应急救援演练
 */

export class RescueDrillPage implements OnInit {
   list: any=[];//返回数据列表
  picList=[];
   total : any ;
   rescueDrill: RescueDrill = new RescueDrill();
   data : any ;//传入详细页面数据
   currentModal: NzModalSubject;
   conf = Config;
  drillTypes = Config.DRILL_TYPES;
  partyType : string = '';
  constructor(
    private modalService: NzModalService,
    private router :Router,
    private route:ActivatedRoute,
    private httpServcie:HttpServcie,
    private _notification: NzNotificationService,
  ) {
    
   }

  ngOnInit() {
    this.rescueDrill.pageSize = 10;
    this.rescueDrill.currentPage = 1;
    this.route.paramMap.subscribe(data=>{
            //查询电梯的信息
            this.partyType = JSON.parse(window.localStorage.getItem("user")).partyType;
            if(this.partyType == '1'){
              this.rescueDrill.maintenancePartyId = JSON.parse(window.localStorage.getItem("user")).partyId;
            }
            if(this.partyType == '2'){
              this.rescueDrill.drillPartyId =  JSON.parse(window.localStorage.getItem("user")).partyId;
            }
            this.query();
        });
  }
  //查询
  query(){
		//查询当前电梯的评价
		this.httpServcie.rescueDrillList(this.rescueDrill).subscribe(data=>{
				if(data.status==200){
				  this.list = data.data.list;
				  this.total = data.data.total;
			
				} else if (data.status === 401) {
				  this.router.navigateByUrl('/login');
				  return;
				} else {
				  // 异常
				  this._notification.create('error', '错误提示', data.message);
				}
		  	}, error => {
				console.log(JSON.stringify(error))
			});
	}
  //table加载数据时
  displayDataChange($event) {
    this.list = $event;
  }
  	//分页
  nzPageSizeChange(event) {
		this.rescueDrill.pageSize = event;
        this.rescueDrill.currentPage = 1;
        this.query()
  }

  //输入框的方法
	nzPageIndexChange(event) {
		this.rescueDrill.currentPage = event;
		this.query();
  }
  
  //当前页
	nzPageIndexClickChange(event) {
		this.rescueDrill.currentPage = event;
		this.query();
  }
 
  
  getPics(pic){
    if(pic){
      return pic.split(',');

    }else{
      return [];
    }
  }


	showLargeImage(titlepic,contentpic,footerpic,img){
		
		this.picList=[];
		this.picList.push(img);
		this.currentModal = this.modalService.open({
		title       : titlepic,
		content     : contentpic,
		footer      : footerpic,
		maskClosable: false,
		width:800,
		style : {
			top: '20px'
		},
		onOk() {
			console.log('Click ok');
		}
		});
  }
  export(id) {
    this.httpServcie.exportEmRscDrill(id).subscribe(data => {
      if (data.status == 200) {
        this.download(data.data.fileUrl, data.data.fileName);
      } else {
        this._notification.create("error", "出错了", "导出失败");
      }
    });
  }
  download(url, name) {
    let link = document.createElement("a");
    let body = document.querySelector("body");

    this.getBlob(Config.IP + Config.API.getUpload + url).then(blob => {
      link.href = window.URL.createObjectURL(blob);
      link.download = name;
      // fix Firefox
      link.style.display = "none";
      body.appendChild(link);
      link.click();
      body.removeChild(link);
      window.URL.revokeObjectURL(link.href);
    });
  }
  getBlob(url) {
    return new Promise(resolve => {
      const xhr = new XMLHttpRequest();
      xhr.open("GET", url, true);
      xhr.responseType = "blob";
      xhr.onload = () => {
        if (xhr.status === 200) {
          resolve(xhr.response);
        }
      };
      xhr.send();
    });
  }
}
